const {
	app,
	BrowserWindow
} = require('electron');
const { del } = require('./cook');
const ipcMain = require('electron').ipcMain;
const Session = require('./cook');
const Menu = require('electron').Menu;
const httpUrl = 'http://81.71.136.172:8080';
const Url = './app/index.html';
var wins;
var starW = 720;
var starH = 520;
var zongW = 0;zongH = 0,type=false;
console.log(encodeURI(Url+'#/login'))
app.on('ready', function() {
	Menu.setApplicationMenu(null);
	wins = new BrowserWindow({
		width: starW,
		height: starH,
		allowDisplayingInsecureContent :true,//允许一个使用 https的界面来展示由 http URLs 传过来的资源
		allowRunningInsecureContent:true,//允许一个使用 https的界面来渲染由 http URLs 提交的html,css,javascript
		experimentalFeatures:true,//开启 Chromium 的 可测试 特性
		experimentalCanvasFeatures:true,//开启 Chromium 的 canvas 可测试特性
		defaultFontSize:10,//默认字体
		minimumFontSize:6,//最小字体
		plugins:true,//插件支持
		frame:false,//隐藏菜单栏
		defaultFontFamily:'sansSerif',//默认字体样式
		// standard String - 默认为 Times New Roman.
		// serif String - 默认为 Times New Roman.
		// sansSerif String - 默认为 Arial.
		//monospace String - 默认为 Courier New.
		webgl:true,//webgl支持
		webaudio:true,//开启 WebAudio 支持
		fullscreenable:false,
		webPreferences: {
			webSecurity:false,
			nodeIntegration: true,
			enableRemoteModule:true
		}
	})
	// 并且为你的应用加载index.html/loadFile
	// win.loadURL('./index.html')
	// wins.loadFile('./index.html')
	wins.setMinimizable(false);
	wins.setMaximizable(false);
	wins.setResizable(false);
	wins.setMenu(null);//隐藏工具栏
	// wins.loadURL(Url+'#/login');
	wins.loadFile('./index.html');

	// 打开开发者工具
	wins.webContents.openDevTools();
	var newpost = wins.getPosition();
	zongW = newpost[0] * 2 + starW;
	zongH = newpost[1] * 2 + starH;
	// console.log(wins.getPosition())
})

ipcMain.on('login', function(event, arg) {
	// console.log(event, arg);
	
	// wins.loadURL(httpUrl+'#/login');
	wins.loadFile(Url+'#/login');
	wins.setPosition((zongW - starW)/2, (zongH - starH)/2,true)
	wins.setSize(starW,starH,true);
	wins.setResizable(false);
	wins.setMinimizable(false);
	wins.setMaximizable(false);
	// console.log(starW,starH)
})
ipcMain.on('index', function(event, arg) {
	// console.log(event, arg);
	wins.setResizable(true);
	wins.setMinimizable(true);
	wins.setMaximizable(true);
	wins.loadURL(httpUrl);
	// wins.loadFile(Url+'#/index');
	wins.setPosition((zongW - 1100)/2, (zongH - 600)/2,true)
	wins.setSize(1100,600,true);
});
ipcMain.on('closeWin', function(event, arg) {//窗口关闭
	wins.destroy();
})
ipcMain.on('max', function(event, arg) {//窗口最大化
	type = !type;
	if(type){
		wins.maximize();
	}else{
		wins.setPosition((zongW - 1100)/2, (zongH - 600)/2,true)
		wins.setSize(1100,600,true);
	}
	
	// wins.setFullScreen(true);//窗口全屏
})
ipcMain.on('min', function(event, arg) {//窗口最大化
	wins.minimize();
})
ipcMain.on('restore', function(event, arg) {//将最小化的窗口恢复为之前的状态.
	wins.restore();
})

ipcMain.on('getmsg', function(event, arg) {//返回一个对象，它包含了窗口的宽，高，x坐标，y坐标.
	var data = wins.getBounds();
	// console.log(data)
})
ipcMain.on('$session', function(event, arg) {//将最小化的窗口恢复为之前的状态.
	// console.log(event, arg)
	// console.log(arg)
	if(arg.type == 'del'){
		Session.del(arg['name']);
	}else if(arg.type == 'set'){//end:过期时间/秒，默认关闭清除
		delete arg['type'];
		Session.set(arg);
	}
	else{
		// console.log(22)
		var data = Session.get(arg['name']);
		event.sender.send('ondata',{name:arg['name'],val:data})
	}
	
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
	Session.get(false,true);
	if (process.platform !== 'darwin') {
		app.quit()
	}
})

app.on('activate', () => {
	// On macOS it's common to re-create a window in the app when the
	// dock icon is clicked and there are no other windows open.
	if (BrowserWindow.getAllWindows().length === 0) {
		createWindow()
	}
})
